﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Bus_proj.website
{
    public partial class check_ticket_user : System.Web.UI.Page
    {
        int check;
        string date, time, date2, input_s;
        public string ConvestToDate_check(string s)
        {
            int day = 1;
            int mount = 1;
            int years = 1111;
            try
            {
                string[] stemp = s.Split(' ');
                stemp = stemp[0].Split('/');
                day = Convert.ToInt32(stemp[0]);
                mount = Convert.ToInt32(stemp[1]);
                years = Convert.ToInt32(stemp[2]);
                if (years < 2300)
                {
                    years = years + 543;
                }
                string years2 = years.ToString();
                string mount2 = mount.ToString();
                string day2 = day.ToString();
                return (years2 + "/" + mount2 + "/" + day2);
            }
            catch (Exception)
            {

            }
            string years3 = years.ToString();
            string mount3 = mount.ToString();
            string day3 = day.ToString();
            return (years3 + "/" + mount3 + "/" + day3);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Int32.Parse(Session["login_Access_id"].ToString()) == 1)
                    {

                    }
                    else
                    {
                        Response.Redirect("login.aspx");
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("login.aspx");
                }
                
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
                input_s = TextBox1.Text;
                string drive = @"select *
                            from Table_Sale
                            INNER join Table_Around
                                on Table_Sale.Around_id = Table_Around.Around_id
                            INNER join Table_Route
                                on Table_Around.Route_id = Table_Route.Route_id
                            INNER join Table_Time
                                on Table_Around.Time_id = Table_Time.Time_id
                            inner join Table_SaleDetail
	                                    on Table_SaleDetail.Sale_id = Table_Sale.Sale_id
                            where Table_Sale.Sale_id = '" + input_s + "'";
                SqlCommand sqlcomm = new SqlCommand();
                SqlConnection sqlconn = new SqlConnection(Connection);
                SqlDataAdapter da = new SqlDataAdapter(drive, sqlconn);
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count <= 0)
                {
                    //Panel1.Visible = true;
                    //Label2.Visible = true;
                    string ticket2 = @"select *
                                    from Table_payment
                                    inner join  Table_Sale
                                    on Table_payment.Sale_id = Table_Sale.Sale_id
                                    INNER join Table_Around
                                        on Table_Sale.Around_id = Table_Around.Around_id
                                    INNER join Table_Route
                                        on Table_Around.Route_id = Table_Route.Route_id
                                    INNER join Table_Time
                                        on Table_Around.Time_id = Table_Time.Time_id
                                    inner join Table_SaleDetail
	                                    on Table_SaleDetail.Sale_id = Table_payment.Sale_id
                                     where Table_payment.payment_id = '" + input_s + "' and (SSID =1 or SSID =5) and Table_payment.payment_status = 1 ";
                    SqlCommand sqlcomm2 = new SqlCommand();
                    SqlConnection sqlconn2 = new SqlConnection(Connection);
                    SqlDataAdapter da_ticket2 = new SqlDataAdapter(ticket2, sqlconn);
                    DataTable dt_ticket2 = new DataTable();
                    da_ticket2.Fill(dt_ticket2);
                    if (dt_ticket2.Rows.Count <= 0)
                    {
                        Panel1.Visible = true;
                        Label2.Visible = true;
                    }
                    else
                    {
                        date = ConvestToDate_check(dt_ticket2.Rows[0]["Around_date"].ToString());
                        time = dt_ticket2.Rows[0]["Time_time"].ToString();
                        date2 = date + " " + time + ":00";
                        string testt = ((DateTime.Parse(date2)).AddHours(-4)).ToString();
                        DateTime test = DateTime.Parse(testt);
                        if (test >= DateTime.Now)
                        {
                            GridView1.DataSource = dt_ticket2;
                            GridView1.DataBind();

                            Panel1.Visible = true;
                            //Session["seat_no"] = input_s;

                        }
                        else
                        {
                            Panel1.Visible = false;

                            ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถคืนตั๋วก่อนเดินทาง 4 ชั่วโมงได้');", true);
                            //Response.Write(@"<script language='javascript'>alert('ไม่สามารถคืนตั๋วได้');</script>");
                        }
                    }
                }
                else
                {
                    Label2.Visible = false;
                    Panel1.Visible = true;
                    string date = ConvestToDate_check(dt.Rows[0]["Around_date"].ToString());
                    string time = dt.Rows[0]["Time_time"].ToString();
                    string date2 = date + " " + time + ":00";
                    if (DateTime.Parse(date2) >= DateTime.Now)
                    {
                        GridView1.DataSource = dt;
                        GridView1.DataBind();

                    }
                    else
                    {
                        Panel1.Visible = false;
                        ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('รอบรถออกเดินทางแล้ว');", true);
                    }
                }
            }
            catch (Exception)
            {
                Panel1.Visible = false;
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('กรุณาป้อนข้อมูล');", true);
            }   
        }
    }
}